package com.example.wanandroid.activity.main

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.example.wanandroid.R
import com.example.wanandroid.activity.main.home.HomeFragment
import com.example.wanandroid.activity.main.person.PersonFragment
import com.example.wanandroid.activity.main.project.ProjectFragment
import com.example.wanandroid.activity.main.tree.TreeFragment
import com.example.wanandroid.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private val viewModel:MainViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val homeItem = BottomNaviView.NaviItem(R.drawable.home,R.drawable.home_selected, "主页")
        val projectItem = BottomNaviView.NaviItem(R.drawable.reorder,R.drawable.reorder_selected, "项目")
        val treeItem = BottomNaviView.NaviItem(R.drawable.list, R.drawable.list_selected,"体系")
        val personItem = BottomNaviView.NaviItem(R.drawable.person, R.drawable.person_selected, "个人")

        val items = ArrayList<BottomNaviView.NaviItem>()
        items.add(homeItem)
        items.add(projectItem)
        items.add(treeItem)
        items.add(personItem)
        binding.bottomNavi.addNaviItems(items)

        val fragments = ArrayList<Fragment>()
        fragments.add(HomeFragment.newInstance())
        fragments.add(ProjectFragment.newInstance())
        fragments.add(TreeFragment.newInstance())
        fragments.add(PersonFragment.newInstance())
        binding.viewPager.adapter = MainAdapter(this, fragments)
        binding.viewPager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                binding.bottomNavi.setSelectedPosition(position)
            }
        })
        binding.bottomNavi.setOnItemClickListener(object : BottomNaviView.OnItemClickListener{
            override fun onItemClicked(view: View, position: Int) {
                binding.viewPager.currentItem = position
            }
        })
    }
}